package gin

import (
	"context"
	"net/http"

	coreError "core/contract/error"
	"core/contract/log"
	"core/enum/response"
	iCoreResponse "gateway/contract/response"

	"github.com/gin-gonic/gin"
)

type ResponseSvc struct {
	logFactory log.ILogFactory
}

type responseBody struct {
	Code    response.Value `json:"code"`
	Message string         `json:"message"`
	Data    interface{}    `json:"data"`
}

func (r ResponseSvc) Regular(ctx context.Context, data interface{}, err coreError.IError) {
	g := ctx.(*gin.Context)
	code := response.Success
	if err != nil {
		code = err.GetCode()
		data = err.GetData()
	}

	g.JSON(http.StatusOK, responseBody{
		Code:    code,
		Message: response.CodeMap[code],
		Data:    data,
	})
}

func NewResponseSvc(logFactory log.ILogFactory) iCoreResponse.IResponse {
	return ResponseSvc{
		logFactory: logFactory,
	}
}
